<?php
namespace app\api\controller\foodcash\shop;

use app\api\controller\foodcash\Controller;
use app\api\model\foodcash\ShopClerk as ClerkModel;

/**
 * 店员管理控制器
 */
class Clerk extends Controller
{
    
    /**
     * 列表 - 不分页
     */
    public function getAll()
    {
        $model = new ClerkModel;
        $clerk = $model->getAll($this->shop_id);
        return $this->renderSuccess(compact('clerk'));
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * 列表
     */
    public function index()
    {
        $model = new ClerkModel;
        $list = $model->getList();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
		$model = new ClerkModel;
		$data = $this->request->post();
        $data['shop_id'] = $data['s_id'];
        if($model->add($data)){
			return $this->renderMsg('添加成功');
		}
		$error = $model->getError() ?: '添加失败';
		return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = ClerkModel::get($id);
        if ($model->remove()) {
            return $this->renderMsg('删除成功');
		}
		$error = $model->getError() ?: '删除失败';
		return $this->renderError($error);
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        // 详情
        $detail = ClerkModel::get($id,['shop']);
        if (!$this->request->isPost()) {
            return $this->renderSuccess(compact('detail'));
        }
        $data = $this->request->post();
        $data['shop_id'] = $data['s_id'];
        // 更新记录
        if ($detail->edit($data)) {
            return $this->renderMsg('更新成功');
        }
        $error = $detail->getError() ?: '更新失败';
        return $this->renderError($error);
    }
}
